package com.sqlist.shareserver.resolver;

import com.sqlist.shareserver.dto.MessageDTO;
import com.sqlist.shareserver.util.GsonUtils;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;


/**
 * Created by SqList on 2018/6/13.
 */

@Slf4j
@Component
public class DataResolverProxy implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public void doAction(String messageString, Channel channel) {
        log.info("doAction" + messageString);

        MessageDTO messageDTO = GsonUtils.getInstance().fromJson(messageString, MessageDTO.class);

        String type = messageDTO.getType();
        DataResolver dataResolver = (DataResolver) applicationContext.getBean(type + "DataResolver");
        if (dataResolver != null) {
            dataResolver.resolve(messageString, channel);
        }
    }
}
